﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace Thksoft.Module.ProjectDetails
{
    public partial class IncomeBusiness : BasePage
    {
        private QS_PROJECTDETAILBLL probll = new QS_PROJECTDETAILBLL();

        /// <summary>
        /// 判断是否首次加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //判断是否是只查询
                if (Request.QueryString["Select"] == "sel")
                {
                    //btnSum.Style.Add("display", "none");
                    btnOk.Style.Add("display", "none");
                    gvMonthPlan.Enabled = false;
                    mytr.Attributes.Add("style", "display:none");
                    //btnDaochu.CssClass = "bt_css_s";
                }

                //判断是否可修改
                if (String.Equals(Request.QueryString["Select"], "no", StringComparison.CurrentCultureIgnoreCase))
                {
                    btnOk.CssClass = "hide";
                    mytr.Attributes.Add("class", "hide");
                }

                if (Request.QueryString["Select"] != "sel")
                {
                    gvMonthPlan.Columns[8].Visible = false;
                    gvMonthPlan.Columns[9].Visible = false;
                }

                BindInit();
            }
        }

        /// <summary>
        /// 绑定加载数据
        /// </summary>
        public void BindInit()
        {
            var bll = new QS_PLAN_DETAIL1_BLL();
            List<QS_PLAN_DETAIL1> data = null;
            var applyId = Convert.ToInt32(Request.QueryString["ApplyId"]);
            var proId = Convert.ToInt32(Request.QueryString["ProId"]);

            var edit = Request.QueryString["Edit"] != "1" ? false : true;
            switch (Request.QueryString["State"])
            {
                case "1":
                    //data = bll.FindRangeYear1(applyId);
                    break;

                case "2":
                    data = bll.FindRangeMonth1(applyId, proId, 2);//月资金汇总
                    break;

                case "3":
                    data = bll.FindRangeMonth1(applyId, proId, 3);//周资金汇总
                    break;

                case "4":
                    if (string.IsNullOrEmpty(Request.QueryString["userId"]))
                    {
                        data = bll.FindRangeMonth1(applyId, proId, 4);//月资金部门
                    }
                    else
                    {
                        data = bll.FindRangeMonth1(applyId, proId, 4, Request.QueryString["userId"]);//月资金部门
                    }
                    break;

                case "5":
                    if (string.IsNullOrEmpty(Request.QueryString["userId"]))
                    {
                        data = bll.FindRangeMonth1(applyId, proId, 5);//周资金部门
                    }
                    else
                    {
                        data = bll.FindRangeMonth1(applyId, proId, 5, Request.QueryString["userId"]);//周资金部门
                    }
                    break;

                default:
                    break;
            }

            //decimal sum = 0;
            //sum = bll.FindSelectbyId("T1.C_VALUE6", "T1", Request.QueryString["State"], applyId);
            //data.Add(new QS_PLAN_DETAILS1 { Index = -1, SumPrice = sum });
            gvMonthPlan.DataSource = data;
            gvMonthPlan.DataBind();
        }

        //确定
        protected void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                var entitys = new List<QS_PLAN_DETAIL1>();
                var applyId = Convert.ToInt32(Request.QueryString["ApplyId"]);
                var proId = Convert.ToInt32(Request.QueryString["ProId"]);

                foreach (GridViewRow row in gvMonthPlan.Rows)
                {
                    //判断属于最后一行剔除
                    //if (row.RowIndex == gvMonthPlan.Rows.Count - 1)
                    //{
                    //    continue;
                    //}
                    var c1 = row.Cells[1].Controls[1] as TextBox;
                    var c2 = row.Cells[2].Controls[1] as DropDownList;
                    var c3 = row.Cells[3].Controls[1] as TextBox;
                    var c4 = row.Cells[4].Controls[1] as TextBox;
                    var c5 = row.Cells[5].Controls[1] as TextBox;
                    var c6 = row.Cells[6].Controls[1] as TextBox;
                    var c7 = row.Cells[7].Controls[1] as TextBox;
                    var entity = new QS_PLAN_DETAIL1();
                    if (c1.Text.Trim() == "")
                    {
                        MessageBox.Show(this, "请先完善第" + (row.RowIndex + 1) + "数据");
                        return;
                    }

                    entity.N_PRO_ID = proId;
                    entity.N_TYPE = decimal.Parse(Request.QueryString["State"].ToString());
                    entity.N_PLANAPP_ID = applyId;
                    entity.N_NUM = decimal.Parse(c1.Text);

                    entity.C_PRONAME = c2.SelectedItem.Text;

                    entity.N_DOSAGE = c3.Text == "" ? 0 : decimal.Parse(c3.Text);
                    entity.N_PRICE = c4.Text == "" ? 0 : decimal.Parse(c4.Text);
                    entity.N_AMOUNT = c5.Text == "" ? 0 : decimal.Parse(c5.Text);

                    entity.C_BANK = c6.Text;

                    entity.C_REMARK = c7.Text;
                    entitys.Add(entity);
                }

                var bll = new QS_PLAN_DETAIL1_BLL();

                switch (Request.QueryString["State"])
                {
                    case "1":
                        //bll.UpdateOrCreateRangeYear1(applyId, proId, entitys);
                        break;

                    case "2":
                        bll.UpdateOrCreateRangeMonth1(applyId, proId, 2, entitys);
                        break;

                    case "3":
                        bll.UpdateOrCreateRangeMonth1(applyId, proId, 3, entitys);
                        break;

                    case "4":
                        bll.UpdateOrCreateRangeMonth1(applyId, proId, 4, entitys);
                        break;

                    case "5":
                        bll.UpdateOrCreateRangeMonth1(applyId, proId, 5, entitys);
                        break;

                    default:
                        break;
                }
                MessageBox.ResponseScript(this, "app1.ReLoad();");
                MessageBox.ResponseScript(this, "FORM_CLOSE();");
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "提交失败");
            }
        }

        //求和
        protected void btnSum_Click(object sender, EventArgs e)
        {
            //Sum();
        }

        //设置样式
        protected void gvMonthPlan_RowDataBound1(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex == -1)
            {
                return;
            }

            #region 绑定下拉列表框数据

            //项目名称
            var proId = Convert.ToDecimal(Request.QueryString["ProId"]);

            var where = string.Format(@" AND C_TABLETYPE = '{0}' AND N_NAMETYPE = {1} ", "T1", 1);
            var dt4 = new QS_PlanDetail_DropLIstBLL().SelectPlanDropList(where, proId);

            if (((DropDownList)e.Row.FindControl("FORM_ProjectName")) != null)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("FORM_ProjectName");
                ddl.Items.Clear();
                if (dt4 != null)
                {
                    for (int i = 0; i < dt4.Rows.Count; i++)
                    {
                        ddl.Items.Insert(0, new ListItem(dt4.Rows[i][1].ToString(), dt4.Rows[i][0].ToString()));
                    }
                }
                //  ddl.Items.Insert(0, new ListItem("---请选择---", "-1"));
            }

            #endregion 绑定下拉列表框数据

            QS_PLAN_DETAIL1 _row = e.Row.DataItem as QS_PLAN_DETAIL1;
            var row = e.Row;
            if (_row.N_TYPE == -1)//如果数据为Null则代表为合计行
            {
                row.Cells[1].Text = "";
                row.Cells[2].Text = "合计";
                row.Cells[3].Text = "";
                row.Cells[4].Text = "";
                row.Cells[5].Text = "";
                row.Cells[6].Text = "";
                row.Cells[7].Text = "";
                return;
            }

            if (_row.N_TYPE == -2)
            {
                var cc1 = row.Cells[1].Controls[1] as TextBox;
                cc1.Text = _row.N_NUM.ToString();
                return;
            }
            var c1 = row.Cells[1].Controls[1] as TextBox;
            var c2 = row.Cells[2].Controls[1] as DropDownList;
            var c3 = row.Cells[3].Controls[1] as TextBox;
            var c4 = row.Cells[4].Controls[1] as TextBox;
            var c5 = row.Cells[5].Controls[1] as TextBox;
            var c6 = row.Cells[6].Controls[1] as TextBox;
            var c7 = row.Cells[7].Controls[1] as TextBox;

            c1.Text = _row.N_NUM.ToString();
            try
            {
                c2.Items.FindByText(_row.C_PRONAME).Selected = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "序号【" + _row.N_NUM + "】中收入类别原本的【" + _row.C_PRONAME + "】内容已不存在，请重新选择！");
            }

            c3.Text = _row.N_DOSAGE == 0 ? "" : _row.N_DOSAGE.ToString();
            c4.Text = _row.N_PRICE == 0 ? "" : _row.N_PRICE.ToString();
            c5.Text = _row.N_AMOUNT == 0 ? "" : _row.N_AMOUNT.ToString();

            c6.Text = _row.C_BANK;
            c7.Text = _row.C_REMARK;

            if (String.Equals(Request.QueryString["Select"], "no", StringComparison.CurrentCultureIgnoreCase))
            {
                var c8 = row.Cells[8].Controls[1] as Label;
                var c9 = row.Cells[9].Controls[1] as Label;
                c8.Text = _row.N_AMT == 0 ? "" : _row.N_AMT.ToString();
                c9.Text = _row.C_AMTPRO;
            }
        }

        #region 添加行

        /// <summary>
        /// 添加行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var dt = new List<QS_PLAN_DETAIL1>();
            //  decimal sum = 0;

            //递归把数据从页面上抓取
            foreach (GridViewRow row in gvMonthPlan.Rows)
            {
                //判断属于最后一行剔除
                //if (row.RowIndex == gvMonthPlan.Rows.Count - 1)
                //{
                //    continue;
                //}
                var c1 = row.Cells[1].Controls[1] as TextBox;
                var c2 = row.Cells[2].Controls[1] as DropDownList;
                var c3 = row.Cells[3].Controls[1] as TextBox;
                var c4 = row.Cells[4].Controls[1] as TextBox;
                var c5 = row.Cells[5].Controls[1] as TextBox;
                var c6 = row.Cells[6].Controls[1] as TextBox;
                var c7 = row.Cells[7].Controls[1] as TextBox;
                var entity = new QS_PLAN_DETAIL1();
                if (c1.Text.Trim() == "")
                {
                    MessageBox.Show(this, "请先完善第" + (row.RowIndex + 1) + "数据");
                    return;
                }

                entity.N_NUM = decimal.Parse(c1.Text);
                if (c2.SelectedItem != null)
                {
                    entity.C_PRONAME = c2.SelectedItem.Text;
                }
                else
                {
                    MessageBox.Show(this, "请先完善第" + (row.RowIndex + 1) + "数据");
                    return;
                }

                entity.N_DOSAGE = c3.Text == "" ? 0 : decimal.Parse(c3.Text);
                entity.N_PRICE = c4.Text == "" ? 0 : decimal.Parse(c4.Text);
                entity.N_AMOUNT = c5.Text == "" ? 0 : decimal.Parse(c5.Text);

                entity.C_BANK = c6.Text;

                entity.C_REMARK = c7.Text;

                dt.Add(entity);
            }

            dt.Add(new QS_PLAN_DETAIL1 { N_NUM = gvMonthPlan.Rows.Count + 1, N_TYPE = -2 });

            gvMonthPlan.DataSource = dt;
            gvMonthPlan.DataBind();
        }

        #endregion 添加行

        #region 删除行

        /// <summary>
        /// 删除行（通过行的索引删除,如果索引为空则删除最后一行）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDel_Click(object sender, EventArgs e)
        {
            var dt = new List<QS_PLAN_DETAIL1>();
            //递归把数据从页面上抓取
            foreach (GridViewRow row in gvMonthPlan.Rows)
            {
                //判断属于最后一行剔除
                //if (row.RowIndex == gvMonthPlan.Rows.Count - 1)
                //{
                //    continue;
                //}
                var c1 = row.Cells[1].Controls[1] as TextBox;
                var c2 = row.Cells[2].Controls[1] as DropDownList;
                var c3 = row.Cells[3].Controls[1] as TextBox;
                var c4 = row.Cells[4].Controls[1] as TextBox;
                var c5 = row.Cells[5].Controls[1] as TextBox;
                var c6 = row.Cells[6].Controls[1] as TextBox;
                var c7 = row.Cells[7].Controls[1] as TextBox;
                var entity = new QS_PLAN_DETAIL1();

                entity.N_NUM = decimal.Parse(c1.Text);

                entity.C_PRONAME = c2.SelectedItem.Text;

                entity.N_DOSAGE = c3.Text == "" ? 0 : decimal.Parse(c3.Text);
                entity.N_PRICE = c4.Text == "" ? 0 : decimal.Parse(c4.Text);
                entity.N_AMOUNT = c5.Text == "" ? 0 : decimal.Parse(c5.Text);

                entity.C_BANK = c6.Text;

                entity.C_REMARK = c7.Text;

                dt.Add(entity);
            }
            //dt.Add(new QS_PLAN_DETAILS1 { Index = -1 });
            if (dt.Count < 1)
            {
                return;
            }
            //if (p_id.Value == "")
            //{
            //    //删除最后一行
            //    dt.RemoveAt(dt.Count - 1);
            //}
            //else
            //{
            //    if (Convert.ToInt32(p_id.Value) > 0 && Convert.ToInt32(p_id.Value) < dt.Count)
            //    {
            //        dt.RemoveAt(Convert.ToInt32(p_id.Value) - 1);
            //    }

            //}
            int count = 0;
            for (int i = 0; i < gvMonthPlan.Rows.Count; i++)
            {
                //bool isChecked = ((CheckBox)(gvMonthPlan.HeaderRow.Cells[0].FindControl("chkSelectAll"))).Checked;
                //if (isChecked == true)
                //{
                //    dt.RemoveAt(0);
                //}
                //else
                //{
                if (((CheckBox)gvMonthPlan.Rows[i].Cells[0].FindControl("chkSelectId")).Checked == true)
                {
                    CheckBox cb = ((CheckBox)gvMonthPlan.Rows[i].Cells[0].FindControl("chkSelectId")) as CheckBox;
                    GridViewRow row = cb.NamingContainer as GridViewRow;
                    int id = row.RowIndex;

                    if (id > 0)
                    {
                        id = id - count;
                    }

                    dt.RemoveAt(id);
                    count++;
                }
                //}
            }
            gvMonthPlan.DataSource = dt;
            gvMonthPlan.DataBind();
        }

        #endregion 删除行

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDaochu_Click(object sender, EventArgs e)
        {
        }
    }
}